package com.woniu.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.dao.RbacRoleDao;
import com.woniu.entity.RbacRole;
import com.woniu.entity.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author WenXin
 * @version 1.0
 * @description: TODO
 * @date 2022/9/2 16:59
 */
@Service
public class RbacRoleServiceImpl implements RbacRoleService {
    @Autowired
    RbacRoleDao rbacRoleDao;

    @Override
    public ResponseEntity findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RbacRole> all = rbacRoleDao.findAll();
        PageInfo pageInfo = new PageInfo(all);
        return new ResponseEntity(200, "主页初始化成功", pageInfo);
    }

    @Override
    public int addOne(RbacRole rbacRole) {

        return rbacRoleDao.addOne(rbacRole);
    }

    @Override
    public int deleteById(int id) {

        return rbacRoleDao.deleteById(id);
    }

    //当角色或权限信息发生改变时，可以清空缓存,allEntries是否清空全部 默认为false
    @CacheEvict(value = {"cacheRole", "cachePerm"}, allEntries = true)
    @Override
    public int update(RbacRole rbacRole) {

        return rbacRoleDao.update(rbacRole);
    }

    //模糊搜索，含分页
    @Override
    public ResponseEntity searchByRole(RbacRole rbacRole, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RbacRole> rbacRoles = rbacRoleDao.searchByRole(rbacRole);
        PageInfo<RbacRole> rbacRolePageInfo = new PageInfo<>(rbacRoles);
        return new ResponseEntity(200, "搜索结果如下", rbacRolePageInfo);
    }

    @Override
    public Set<String> findOneByUserName(String username) {

        return rbacRoleDao.findOneByUserName(username);
    }

    @Override
    public ResponseEntity findAll() {
        List<RbacRole> all = rbacRoleDao.findAll();
        return new ResponseEntity(200, "查询结果如下", all);
    }


}
